package com.lingshi.shopping_user_service.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lingshi.shopping.constant.Constant;
import com.lingshi.shopping.constant.RedisKey;
import com.lingshi.shopping.entity.ShoppingUser;
import com.lingshi.shopping.enums.ErrorEnums;
import com.lingshi.shopping.exception.BusException;
import com.lingshi.shopping.service.ShoppingUserService;
import com.lingshi.shopping_user_service.mapper.ShoppingUserMapper;
import com.lingshi.shopping_user_service.util.JwtUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.Duration;
import java.util.Objects;

@DubboService
public class ShoppingUserServiceImpl implements ShoppingUserService {

    @Autowired
    private ShoppingUserMapper shoppingUserMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void saveRegisterCheckCode(String phone, String checkCode) {

        String key = StrUtil.format(RedisKey.REGISTER_KEY, phone);

        stringRedisTemplate.opsForValue().set(key, checkCode, Duration.ofSeconds(180));

    }

    @Override
    public void registerCheckCode(String phone, String checkCode) {

        String key = StrUtil.format(RedisKey.REGISTER_KEY, phone);

        if(stringRedisTemplate.hasKey(key)){
            String redisCode = stringRedisTemplate.opsForValue().get(key);
            if(!checkCode.equals(redisCode)){
                throw new BusException(ErrorEnums.CHECK_CODE_ERROR);
            }else {
                stringRedisTemplate.delete(key);
            }
        }else {
            throw new BusException(ErrorEnums.CHECK_CODE_ERROR);
        }

    }

    @Override
    public void register(ShoppingUser shoppingUser) {
        /**
         * 1， 检查手机号是否注册
         *   是： 抛逻辑异常
         * 2， 检查账号是否注册
         *   是： 抛逻辑异常
         */

        LambdaUpdateWrapper<ShoppingUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingUser::getPhone, shoppingUser.getPhone());
        Long count = shoppingUserMapper.selectCount(wrapper);
        if(count > 0){
            throw new BusException(ErrorEnums.REGISTER_PHONE_EXISTS_ERROR);
        }

        LambdaUpdateWrapper<ShoppingUser> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(ShoppingUser::getUsername, shoppingUser.getUsername());
        Long count1 = shoppingUserMapper.selectCount(wrapper1);
        if(count1 > 0){
            throw new BusException(ErrorEnums.REGISTER_USERNAME_EXISTS_ERROR);
        }


        shoppingUser.setStatus(Constant.Y);
        //密码加密
        String md5 = SecureUtil.md5(shoppingUser.getPassword());
        shoppingUser.setPassword(md5);

        shoppingUserMapper.insert(shoppingUser);
    }

    @Override
    public String loginPassword(String username, String password) {

        LambdaQueryWrapper<ShoppingUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingUser::getUsername, username);
        wrapper.eq(ShoppingUser::getPassword, SecureUtil.md5(password));
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(wrapper);
        if(Objects.isNull(shoppingUser)){
            throw new BusException(ErrorEnums.LOGIN_ERROR);
        }
        //生成令牌
        return JwtUtils.sign(shoppingUser.getId(), shoppingUser.getUsername());
    }

    @Override
    public void saveLoginCheckCode(String phone, String checkCode) {

        String key = StrUtil.format(RedisKey.LOGIN_KEY, phone);

        if(stringRedisTemplate.hasKey(key)){
            throw new BusException(ErrorEnums.SEND_CODE_FAST_ERROR);
        }

        stringRedisTemplate.opsForValue().set(key, checkCode, Duration.ofSeconds(180));

    }

    @Override
    public String loginCheckCode(String phone, String checkCode) {

        String key = StrUtil.format(RedisKey.LOGIN_KEY, phone);
        if(stringRedisTemplate.hasKey(key)){
            String redisCode = stringRedisTemplate.opsForValue().get(key);
            if(!checkCode.equals(redisCode)){
                throw new BusException(ErrorEnums.CHECK_CODE_ERROR);
            }else {
                stringRedisTemplate.delete(key);
            }
        }else {
            throw new BusException(ErrorEnums.CHECK_CODE_ERROR);
        }
        //根据手机号查询用户信息
        LambdaQueryWrapper<ShoppingUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingUser::getPhone, phone);
        ShoppingUser shoppingUser = shoppingUserMapper.selectOne(queryWrapper);
        //TODO 使用JWT 生成token
        //生成令牌
        return JwtUtils.sign(shoppingUser.getId(), shoppingUser.getUsername());
    }

    @Override
    public String getName(String token) {

        return "";
    }

    @Override
    public ShoppingUser getLoginUser(Long id) {
        return null;
    }
}
